package com.snow.useviewdemo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadCacheTools {
    volatile static ExecutorService mExecutorService = null;
    private static List<Runnable> mList = new ArrayList<>();

    private static void init() {
        if (null == mExecutorService) {
            synchronized (ThreadCacheTools.class) {
                if (null == mExecutorService) {
//                    mExecutorService = Executors.newCachedThreadPool();
                    mExecutorService = Executors.newFixedThreadPool(5);
//                    mExecutorService = Executors.newSingleThreadExecutor();
//                    mExecutorService = Executors.newScheduledThreadPool(5);
                }
            }
        }
    }

    public static void newThread(Runnable runnable) {
        if (null == mExecutorService) {
            init();
        }
        mExecutorService.execute(runnable);
    }

    public static void addTask(Runnable runnable) {
        mList.add(runnable);
    }

    public static void execute() {
        if (mList == null) {
            return;
        }
        for (Runnable runnable : mList) {
            if (runnable == null) {
                continue;
            }
            newThread(runnable);
        }
    }

}
